import { defineStore } from 'pinia'

export const useTagsStore = defineStore('tags',{
    state: () => {
        return {
            tags: JSON.parse(sessionStorage.getItem('tags')) || [],
            activeRouteName:sessionStorage.getItem('activeRouteName') || "Hello",
            isRefresh:false,
            isCloseTag:false,
            excludeArr:[],
            nowCloseTag:''
        }
    },
    actions:{
        setExcludeArr(value){
            this.excludeArr = value;
        },
        setTags(log) {

            let isExistIndex = this.tags.findIndex(value=>value.fullPath == log.fullPath);
            if(isExistIndex == -1){
                this.tags.push(log);
            }else{
                this.tags[isExistIndex] = log;
            }
            sessionStorage.setItem(`tags`, JSON.stringify(this.tags))
        },
        setNowCloseTag(tag){
            this.nowCloseTag = tag;
        },
        removeTags(log) {
            let isExistIndex = this.tags.findIndex(value=>value.fullPath == log.fullPath);
            if(isExistIndex != -1){
                this.tags.splice(isExistIndex,1);
            }
            sessionStorage.setItem(`tags`, JSON.stringify(this.tags))
            this.nowCloseTag = log.name;
        },
        clearTags() {
            this.tags = [];
            sessionStorage.removeItem(`tags`)
        },
        changeActiveRouteName(name){
            this.activeRouteName = name;
            sessionStorage.setItem(`activeRouteName`,name)
        },
        clearActiveRouteName(){
            this.activeRouteName = "";
            sessionStorage.removeItem(`activeRouteName`)
        },
        changeIsRefresh(flag){

            this.isRefresh = flag;
        },
        changeCloseTag(){
            this.isCloseTag = !this.isCloseTag;
        }
    }
})
